import { useLocation } from 'react-router-dom'
import { isAuth } from '@/utils/storage'
import { Route, Redirect } from 'react-router-dom'
export default function PrivateRoute({
  children,
  component: Component,
  ...rest
}) {
  const location = useLocation()
  return (
    <Route
      {...rest}
      render={() => {
        if (isAuth()) {
          return children ? children : <Component />
        } else {
          /* Redirect 跳转默认是 replace 的效果，也可以添加 push 属性变成 push 的效果 */
          return (
            <Redirect
              to={{
                // 指定跳转地址
                pathname: '/login',
                // 通过 state 来传递额外的参数
                state: {
                  // 把当前的地址作为 from 属性传递了过去
                  from: location.pathname,
                },
              }}
            />
          )
        }
      }}
    />
  )
}
